package com.example.maple.ccsclient.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.utils.SharedPreferencesUtils;

public class SetColumnActivity extends Activity {

	String title;
	Button mButton;
	TextView explain;
	TextView columnValue;
	SeekBar mSeekBar;
	TextView titleTextView;


	boolean isPicSetting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_column);

		findView();
	}

	private void findView() {
		findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				finish();
			}
		});
		Intent intent = getIntent();
		title = intent.getStringExtra("title");
		mSeekBar = (SeekBar) findViewById(R.id.seekBar);
		columnValue = (TextView) findViewById(R.id.column);
		if (title.equals("图片列设置")) {
			isPicSetting = true;
			mSeekBar.setProgress(SharedPreferencesUtils.getPicColumn(this));
			columnValue.setText(String.valueOf(mSeekBar.getProgress()));
		} else {
			isPicSetting = false;
			mSeekBar.setProgress(SharedPreferencesUtils.getCharColumn(this));
			columnValue.setText(String.valueOf(mSeekBar.getProgress()));
		}

		titleTextView = (TextView) findViewById(R.id.titleText);
		explain = (TextView) findViewById(R.id.explain);


		mButton = (Button) findViewById(R.id.commit);

		titleTextView.setText(title);
		explain.setText(intent.getStringExtra("explain"));

		mButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (isPicSetting) {

					SharedPreferencesUtils.setPicColumn(SetColumnActivity.this,mSeekBar.getProgress());
				} else {

					SharedPreferencesUtils.setCharColumn(SetColumnActivity.this,mSeekBar.getProgress());
				}

				Intent i = getBaseContext().getPackageManager()
						.getLaunchIntentForPackage(getBaseContext().getPackageName());
				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
			}
		});

		mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				columnValue.setText(String.valueOf(progress));
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

			}
		});

	}

	public static void startAction(Context context, String title, String explain) {
		Intent intent = new Intent(context,SetColumnActivity.class);
		intent.putExtra("title",title);
		intent.putExtra("explain",explain);
		context.startActivity(intent);
	}
}
